﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Zombie_Demo.Utilities
{
    public static class MouseWrapper
    {
        #region variables
        private static MouseState mouseState; //The current mouse state
        private static Vector2 worldCordinates; //The world cordinates of the cursor
        private static Vector2 screenCordinates; //The screen cordinates of the cursor
        private static Vector2 changeFromLastFrameWorld; //The change in position of the world value
        private static Vector2 changeFromLastFrameWindow; //The change in position in screen coordinates

        private static TimeSpan toggleTimeLMB; //The amount the left mouse button was depressed
        private static TimeSpan toggleTimeRMB; //The amount of time the right mouse button was depressed
        private static int lastScrollWheel; //The measure of the last scroll wheel
        private static int scrollWheelDelta; //The change of the scroll wheel since the last frame

        private static bool pressedLMB; //If the left mouse button is currently pressed
        private static bool wasPressedLMB; //If LMB was pressed last frame
        private static bool pressedRMB; //IF the right mouse button is currently pressed
        private static bool wasPressedRMB; //If RMB was pressed last frame
        private static DateTime toggleOnLMB; //The time the left mouse was pressed
        private static DateTime toggleOnRMB; //The time the right mouse was pressed

        private static Rectangle mouseBox;
        #endregion

        #region Retrievers
        /// <summary>
        /// Returns the value in world cordinates of the mouse
        /// </summary>
        public static Vector2 WorldCordinates
        {
            get { return worldCordinates; }
        }

        public static Vector2 ScreenCordinates
        {
            get { return screenCordinates; }
        }

        public static int ScrollWheelDelta
        {
            get { return scrollWheelDelta; }
        }

        public static int ScrollWheelValue
        {
            get { return mouseState.ScrollWheelValue; }
        }

        public static Vector2 ChangeFromFrameWorld
        {
            get { return changeFromLastFrameWorld; }
        }

        public static Vector2 ChangedFromFrameWindow
        {
            get { return changeFromLastFrameWindow; }
        }

        public static bool MousePressedLeft
        {
            get { return mouseState.LeftButton == ButtonState.Pressed; }
        }

        public static bool MousePressedRight
        {
            get { return mouseState.RightButton == ButtonState.Pressed; }
        }

        public static TimeSpan LastToggle
        {
            get { return toggleTimeLMB; }
        }

        public static Rectangle MouseBox { get { return mouseBox; } }

        /// <summary>
        /// True if the LMB was pressed in the previous frame
        /// </summary>
        public static bool WasLMBPressed { get { return wasPressedLMB; } }

        /// <summary>
        /// True if the RMB was pressed in the previous frame
        /// </summary>
        public static bool WasRMBPressed { get { return wasPressedRMB; } }
        #endregion

        public static void Init(World.Camera cam)
        {
            mouseState = Mouse.GetState();

            Vector2 world = Vector2.Zero;
            world.X = cam.BoundingBox.X + mouseState.X;
            world.Y = cam.BoundingBox.Y + mouseState.Y;
            world = worldCordinates;

            changeFromLastFrameWindow = Vector2.Zero;
            changeFromLastFrameWorld = Vector2.Zero;

            toggleTimeLMB = TimeSpan.MinValue; //TODO: Bad! sets a HUGE negative value. Try using 0... or somthing...
            scrollWheelDelta = 0;
            lastScrollWheel = mouseState.ScrollWheelValue;

            if (mouseState.LeftButton == ButtonState.Pressed)
                pressedLMB = true;
            else
                pressedLMB = false;

            toggleOnLMB = DateTime.Now;
            mouseBox = new Rectangle(mouseState.X, mouseState.Y, 5, 5);
        }

        /// <summary>
        /// Designed to get the world cordinates using a camera
        /// </summary>
        /// <param name="cam">The camera to find the mouse cords of</param>
        /// <returns>A Vector2 of the mouse in the world</returns>
        public static Vector2 GetWorldCordinates(World.Camera cam)
        {
            Vector2 world = Vector2.Zero;

            world.X = cam.BoundingBox.X + Mouse.GetState().X;
            world.Y = cam.BoundingBox.Y + Mouse.GetState().Y;

            return world;
        }

        public static void Update(World.Camera cam)
        {
            mouseState = Mouse.GetState();

            Vector2 world = Vector2.Zero;
            world.X = cam.BoundingBox.X + mouseState.X;
            world.Y = cam.BoundingBox.Y + mouseState.Y;

            changeFromLastFrameWorld = worldCordinates - world;
            changeFromLastFrameWindow = screenCordinates - new Vector2(mouseState.X, mouseState.Y);

            worldCordinates = world;
            screenCordinates = new Vector2(mouseState.X, mouseState.Y);

            scrollWheelDelta = lastScrollWheel - mouseState.ScrollWheelValue;
            lastScrollWheel = mouseState.ScrollWheelValue;

            if (pressedLMB && mouseState.LeftButton == ButtonState.Pressed)
                wasPressedLMB = true;
            if (!pressedLMB)
                wasPressedLMB = false;

            if (pressedRMB && mouseState.RightButton == ButtonState.Pressed)
                wasPressedRMB = true;
            if (!pressedRMB)
                wasPressedRMB = false;

            if (mouseState.LeftButton == ButtonState.Pressed && !pressedLMB)
            {
                pressedLMB = true;
                toggleOnLMB = DateTime.Now;
            }

            if (mouseState.LeftButton == ButtonState.Released && pressedLMB)
            {
                pressedLMB = false;
                toggleTimeLMB = DateTime.Now - toggleOnLMB;
            }

            if (mouseState.RightButton == ButtonState.Pressed && !pressedRMB)
            {
                pressedRMB = true;
                toggleOnRMB = DateTime.Now;
            }

            if (mouseState.RightButton == ButtonState.Released && pressedRMB)
            {
                pressedRMB = false;
                toggleTimeRMB = DateTime.Now - toggleOnRMB;
            }

            mouseBox.X = mouseState.X;
            mouseBox.Y = mouseState.Y;

        }
    }
}
